package com.roger.tool.utils;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

/**
 * @author lch
 * @version Create time：2017-1-18 类说明:
 */
public class DeviceUtil {

	/**
	 * 获取手机 imei
	 * 
	 * @param context
	 * @return
	 */
	public static String getImei(Context context) {

		String imei = null;
		if (context != null) {

			try {
				TelephonyManager tm = (TelephonyManager) context.getSystemService(Activity.TELEPHONY_SERVICE);
				if (tm != null) {
					imei = tm.getDeviceId();
				}
			} catch (Exception e) {

			}
		}

		if (TextUtils.isEmpty(imei)) {
			imei = "0000";
		}
		return imei;
	}

	/**
	 * 获取手机卡imsi码
	 */
	public static final String getImsi(Context pContext) {

		String imsi = null;

		if (pContext != null) {
			try {
				TelephonyManager tm = (TelephonyManager) pContext.getSystemService(Context.TELEPHONY_SERVICE);
				if (tm != null) {
					imsi = tm.getSubscriberId();
				}
			} catch (Exception e) {

			}

		}

		if (TextUtils.isEmpty(imsi)) {
			imsi = "0000";
		}
		return imsi;
	}

	/**
	 * 获取手机卡 iccid
	 * 
	 * @param context
	 * @return
	 */
	public static final String getIccid(Context context) {
		String iccid = null;
		if (context != null) {
			try {
				TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
				if (tm != null) {
					iccid = tm.getSimSerialNumber();
				}
			} catch (Exception e) {
			}
		}

		if (TextUtils.isEmpty(iccid)) {
			iccid = "0000";
		}
		return null;
	}

	public static String getMacAddress(Context pContext) {
		String macAddress = null;
		if (pContext != null) {
			try {
				WifiManager wifi = (WifiManager) pContext.getSystemService(Context.WIFI_SERVICE);
				WifiInfo info = wifi.getConnectionInfo();
				macAddress = info.getMacAddress();
			} catch (Exception e) {

			}
		}

		if (TextUtils.isEmpty(macAddress)) {
			macAddress = "unknown";
		}
		return macAddress;
	}

	public static String getIpAddress() {
		String ip = null;
		try {
			for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
				NetworkInterface intf = en.nextElement();
				for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
					InetAddress inetAddress = enumIpAddr.nextElement();
					if (!inetAddress.isLoopbackAddress()) {
						ip = inetAddress.getHostAddress().toString();
					}
				}
			}
		} catch (SocketException e) {

		}

		if (TextUtils.isEmpty(ip)) {
			ip = "unknown";
		}
		return ip;
	}

	/**
	 * 获取设备品牌机型信息 如果要用于URL，请先经过编码
	 * 
	 * @return
	 */
	public static String getModel() {

		String model = null;

		try {
			model = Build.MODEL;
		} catch (Exception e) {
		}

		if (TextUtils.isEmpty(model)) {
			model = "unknown";
		}

		return model;
	}

	/**
	 * 获取设备制造商 如果要用于URL，请先经过编码
	 * 
	 * @return
	 */
	public static String getManufacturer() {
		String manufacturer = null;

		try {
			manufacturer = Build.MANUFACTURER;
		} catch (Exception e) {
		}

		if (TextUtils.isEmpty(manufacturer)) {
			manufacturer = "unknown";
		}
		return manufacturer;
	}

	/**
	 * 获取设备操作系统版本信息 如果要用于URL，请先经过编码
	 * 
	 * @return
	 */
	public static String getOSVersion() {
		String mOSVersion = null;
		try {
			mOSVersion = Build.VERSION.RELEASE;
		} catch (Exception e) {
		}
		if (TextUtils.isEmpty(mOSVersion)) {
			mOSVersion = "unknown";
		}
		return mOSVersion;
	}

	public static String getNetworkTypeName(Context pContext) {
		try {
			ConnectivityManager connectivity = (ConnectivityManager) pContext.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo networkInfo = connectivity.getActiveNetworkInfo();
			if (networkInfo == null) {
				return "GPRS";
			}
			String typeName = networkInfo.getTypeName();
			if (typeName.compareTo("MOBILE") == 0 || typeName.compareTo("mobile") == 0) {
				return "GPRS";
			}
			return typeName.trim();
		} catch (Exception e) {

		}
		return "GPRS";
	}

	/**
	 * 获取网络类型
	 * 
	 * @param pContext
	 * @return
	 */
	public static String getNetWorkType(Context pContext) {
		String str = null;
		try {
			ConnectivityManager connectivity = (ConnectivityManager) pContext.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo networkInfo = connectivity.getActiveNetworkInfo();
			if (networkInfo != null && networkInfo.isConnected()) {
				String typeName = networkInfo.getTypeName();
				if (typeName.equalsIgnoreCase("WIFI")) {
					str = "WIFI";
				} else if (typeName.equalsIgnoreCase("MOBILE")) {
					str = ((TelephonyManager) pContext.getSystemService(Activity.TELEPHONY_SERVICE)).getNetworkType() + "";
				} else {
					str = "";
				}
			}
		} catch (Exception e) {
		}
		if (TextUtils.isEmpty(str)) {
			str = "unknown";
		}
		return str;
	}

	/**
	 * 获取网络类型等级 2G\3G\4G\WIFI
	 * 
	 * @param pContext
	 * @return
	 */
	public static String getNetworkClass(Context pContext) {
		return NetworkClassUtil.getNetworkClass(DeviceUtil.getNetWorkType(pContext));
	}

	public static int getUid(Context pContext) {
		try {
			return pContext.getPackageManager().getApplicationInfo(pContext.getPackageName(), 1).uid;
		} catch (Exception e) {

		}
		return -1;
	}

	/**
	 * sim卡是否可读
	 * 
	 * @param pContext
	 * @return
	 */
	public static boolean isCanUseSim(Context pContext) {
		try {
			TelephonyManager telMgr = (TelephonyManager) pContext.getSystemService(Context.TELEPHONY_SERVICE);
			return TelephonyManager.SIM_STATE_READY == telMgr.getSimState();
		} catch (Exception e) {

		}
		return false;
	}

}
